import { defineStore, getActivePinia } from "pinia";



const api = {
    login: (username: string, pwd: number) => {
        return {
            data: 1
        }
    }
}
export const userStore = defineStore({

    id: 'user', // 必备，且唯一
    state: () => {
        return {
            name: 'csc111',
            age: 18
        }
    },
    actions: { // 其中的函数都可以通过this访问到仓库的数据源
        updateName(name: string) {
            // console.log(this.name);
            this.name = name
        },
        async login(username: any, pwd: any) {
            const { data } = await api.login(username, pwd)
            this.setData(data)
            return data
        },
        setData(data: any) {
            console.log(data);
        }
    },
    getters: { // 其中的函数都具有 state 参数
        fullName(state) {
            return state.name + '很帅'
        }
    },
    persist: { // 开启数据缓存
        enabled: true,
        strategies: [ //手动控制缓存
            {
                key: 'my',
                storage: localStorage,
                paths: ['name']
            }
        ]
    }
})